#!/bin/bash

# https://github.com/microsoft/vscode/blob/509cc674f24d87a298d56518c318de67bda357cb/resources/darwin/bin/code.sh

function app_realpath() {
    SOURCE="$1"
    while [ -h "$SOURCE" ]; do
        DIR=$(/usr/bin/dirname "$SOURCE")
        SOURCE=$(/usr/bin/readlink "$SOURCE")
        [[ "$SOURCE" != /* ]] && SOURCE="$DIR/$SOURCE"
    done
    SOURCE_DIR="$( cd -P "$( /usr/bin/dirname "$SOURCE" )" >/dev/null 2>&1 && echo "$PWD" )"
    echo "${SOURCE_DIR%%${SOURCE_DIR#*.app}}"
}

APP_PATH="$(app_realpath "${BASH_SOURCE[0]}")"
if [ -z "$APP_PATH" ]; then
    echo "Unable to determine app path from symlink : ${BASH_SOURCE[0]}"
    exit 1
fi

XTL_CLI=1 "$APP_PATH/Contents/MacOS/xtool" "$@"
